package com.pdsu.alumni.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pdsu.alumni.common.result.Result;
import com.pdsu.alumni.pojo.Honor;
import com.pdsu.alumni.service.IHonorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Acerola
 * @since 2022-01-26
 */
@Api(tags = "校友荣誉控制层")
@RestController
@RequestMapping("/honor")
public class HonorController {
    @Autowired
    private IHonorService honorService;

    @ApiOperation(value = "获取荣誉信息列表")
    @GetMapping("/list")
    @RequiresAuthentication
//  @RequiresPermissions("")
    @ResponseBody
    @CrossOrigin
    public Result getAllUser() {
        QueryWrapper<Honor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 1);
        List<Honor> list = honorService.list(queryWrapper);
        return new Result().success().put(list);
    }

    @ApiOperation(value = "查询个人荣誉信息")
    @PostMapping("/getOne")
    @RequiresAuthentication
    //@RequiresPermissions("")
    @ResponseBody
    @CrossOrigin
    public Result getOne(@RequestBody Map map) {
        //身份证号
        Object object = map.get("idCard");
        String idCard = JSON.parseObject(JSON.toJSONString(object), String.class);
        QueryWrapper<Honor> honorQueryWrapper = new QueryWrapper<>();
        honorQueryWrapper.eq("deleted", 1)
                .eq("h_uid", idCard);
        List<Honor> honorList = honorService.list(honorQueryWrapper);
        return new Result().success("查询成功").put(honorList);
    }

    @ApiOperation(value = "添加荣誉信息")
    @PostMapping("/add")
    @RequiresAuthentication
//    @RequiresPermissions("")
    @ResponseBody
    @CrossOrigin
    public Result addHonor(@RequestBody Map map) {
        Object object = map.get("honor");
        Honor honor = JSON.parseObject(JSON.toJSONString(object), Honor.class);
        honor.setDeleted(1);
        honorService.save(honor);
        return new Result().success("添加成功");
    }

    @ApiOperation(value = "修改荣誉信息")
    @PostMapping("/update")
    @RequiresAuthentication
    //@RequiresPermissions("")
    @ResponseBody
    @CrossOrigin
    public Result updateHonor(@RequestBody Map map) {
        Object object = map.get("honor");
        Honor honor = JSON.parseObject(JSON.toJSONString(object), Honor.class);
        if (honorService.updateById(honor))
            return new Result().success("修改成功");
        else return new Result().error("修改失败");
    }

    @ApiOperation(value = "删除荣誉信息")
    @PostMapping("/delete")
    @RequiresAuthentication
//    @RequiresPermissions("")
    @ResponseBody
    @CrossOrigin
    public Result deleteHonor(@RequestBody Map map) {
        Object object = map.get("honor");
        Honor honor = JSON.parseObject(JSON.toJSONString(object), Honor.class);
        honor.setDeleted(0);
        honorService.updateById(honor);
        return new Result().success("删除成功");
    }

}
